package question.solution733;

import question.SolutionTemplate;

public class Solution733 implements SolutionTemplate {

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        if(image[sr][sc] == newColor) return image;
        fillThePoint(image,sr,sc,image[sr][sc],newColor);
        return image;
    }

    public void fillThePoint(int[][] image, int sr, int sc,int originalColor, int newColor){
        if(image[sr][sc] == originalColor){
            image[sr][sc] = newColor;
            if(sr-1>=0){
                fillThePoint(image,sr-1,sc,originalColor,newColor);
            }
            if(sr+1<image.length){
                fillThePoint(image,sr+1,sc,originalColor,newColor);
            }
            if(sc-1>=0){
                fillThePoint(image,sr,sc-1,originalColor,newColor);
            }
            if(sc+1<image[0].length){
                fillThePoint(image,sr,sc+1,originalColor,newColor);
            }
        }
        return;

    }


    @Override
    public void test() {
        int[][] image = {{0,0,0},{0,1,1}};
        int[][] newImage = floodFill(image,1,1,1);
        System.out.println(newImage);
    }
}
